import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPlay extends StatefulWidget {
  const VideoPlay({Key? key}) : super(key: key);

  @override
  _VideoPlayState createState() => _VideoPlayState();
}

class _VideoPlayState extends State<VideoPlay> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    // _controller = VideoPlayerController.networkUrl(
    //     Uri.parse('https://sample-videos.com/video321/flv/720/big_buck_bunny_720p_1mb.flv'))
    //   ..initialize().then((_) {
    //     print("initStateinitStateinitStateinitState");
    //     // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
    //     setState(() {});
    //   });
    //https://kundihulan.com/
    _controller = VideoPlayerController.networkUrl(
      Uri.parse(
          'https://vip.lzcdn2.com/20220528/7735_258d8679/1200k/hls/mixed.m3u8'),
      videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
    );

    _controller.addListener(() {
      setState(() {});
    });
    _controller.setLooping(true);
    _controller.initialize().catchError((onError){
      print("initializeError:${onError.toString()}");
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("视频播放"),
      ),
      body: Container(
          child: Column(
        children: [
          _controller.value.isInitialized
              ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
              : Container(
                  height: 400,
                  color: Colors.black,
                  child: Center(child: CircularProgressIndicator()),
                ),
          Icon(
            _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
        ],
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _controller.value.isPlaying
                ? _controller.pause()
                : _controller.play();
          });
        },
      ),
    );
  }
}
